/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package cc.altius.web.controller;

import cc.altius.model.City;
import cc.altius.model.GeographicalZone;
import cc.altius.model.MasterZone;
import cc.altius.service.GeoZoneService;
import cc.altius.service.ZoneService;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import javax.servlet.http.HttpServletRequest;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.ModelMap;
import org.springframework.web.bind.annotation.ModelAttribute;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.SessionAttributes;

/**
 *
 * @author sagar
 */
@Controller
@SessionAttributes("cityList")
public class GeographicalZoneController {

    @Autowired
    private ZoneService zoneService;
    @Autowired
    private GeoZoneService geoZoneService;

    @ModelAttribute("cityList")
    public List<City> populateCityList() {
        return this.zoneService.getCityList();
    }

    @ModelAttribute("geoZoneList1")
    public List<GeographicalZone> geoZoneList() {
        return this.geoZoneService.getGeoZoneList();
    }

    @ModelAttribute("masterZoneList")
    public List<MasterZone> masterZoneList() {
        return this.geoZoneService.getMasterZoneList();
    }

    // step one
    @RequestMapping(value = "/geographicalzone/addGeographicalZoneStepOne.htm", method = RequestMethod.GET)
    public String ShowAddGeographicalZoneStepOne(@ModelAttribute("geoZone") GeographicalZone geoZone) {
        return "geographicalzone/addGeographicalZoneStepOne";
    }

    // step two
    @RequestMapping(value = "/geographicalzone/addGeographicalZoneStepOne.htm", method = RequestMethod.POST, params = "_stepTwo")
    public String ShowGetAddGeographicalZoneStepTwo(@ModelAttribute("geoZone") GeographicalZone geoZone, ModelMap modelMap) {
        int cityId = geoZone.getCity().getCityId();
        modelMap.addAttribute("geoZone", geoZone);
        modelMap.addAttribute("city", cityId);
        List<Map<String, Object>> masterZoneListOnCityId = this.geoZoneService.getMasterZoneListByCityId(cityId);
        modelMap.addAttribute("masterZoneListOnCityId", masterZoneListOnCityId);
        return "geographicalzone/addGeographicalZoneStepTwo";
    }

    // save
    @RequestMapping(value = "/geographicalzone/addGeographicalZone.htm", method = RequestMethod.POST, params = "_finish")
    public String SaveAddGEographicalZone(@ModelAttribute("geoZone") GeographicalZone geoZone,
            @RequestParam(value = "selectedMasterZoneIds", required = true, defaultValue = "0") String selectedMasterZoneIds,
            ModelMap modelMap, HttpServletRequest request) {
        int geoZoneId = this.geoZoneService.addGeoZone(geoZone, selectedMasterZoneIds);
        if (geoZoneId != 0) {
            return "redirect:listGeographicalZone.htm?msg=Succesfully added Geographical Zone";
        } else {
            return "redirect:addGeographicalZone.htm?msg=msg.geoZoneError";
        }
    }

    // save
    @RequestMapping(value = "/geographicalzone/addGeographicalZone.htm", method = RequestMethod.POST, params = "_cancel")
    public String CancelAddGEographicalZone(@ModelAttribute("geoZone") GeographicalZone geoZone) {
        geoZone = null;
        return "redirect:listGeographicalZone.htm?msg=Action cancelled";
    }

    @RequestMapping(value = "/geographicalzone/listGeographicalZone.htm")
    public String showListPage(ModelMap modelMap,
            @RequestParam(value = "geographicalZoneId", required = true, defaultValue = "0") int geographicalZoneId,
            @RequestParam(value = "cityId", required = true, defaultValue = "0") int cityId) {
        List<Map<String, Object>> geoZoneList = this.geoZoneService.getGeoZoneList(geographicalZoneId, cityId);
        modelMap.addAttribute("geoZoneList", geoZoneList);
        modelMap.addAttribute("geographicalZoneId", geographicalZoneId);
        modelMap.addAttribute("cityId", cityId);
        return "geographicalzone/listGeographicalZone";
    }

    @RequestMapping(value = "/geographicalzone/editGeographicalZone.htm", method = RequestMethod.GET)
    public String showEditGeoZoneGetPage(
            @RequestParam(value = "geographicalZoneId") int geographicalZoneId, ModelMap modelMap) {
        GeographicalZone geoZone = this.geoZoneService.getGeoZoneByGeoZoneId(geographicalZoneId);
        int cityId = geoZone.getCity().getCityId();
        List<Map<String, Object>> masterZoneListOnCityId = this.geoZoneService.getMasterZoneListByCityId(cityId);
        List<Map<String, Object>> masterZoneList1 = this.geoZoneService.getMasterZoneList(geographicalZoneId);
        modelMap.addAttribute("geoZone", geoZone);
        modelMap.addAttribute("masterZoneList1", masterZoneList1);
        modelMap.addAttribute("masterZoneListOnCityId", masterZoneListOnCityId);
        return "/geographicalzone/editGeographicalZone";
    }

    @RequestMapping(value = "/geographicalzone/editGeographicalZone.htm", method = RequestMethod.POST)
    public String postEditGeographicalZonePage(@ModelAttribute("geoZone") GeographicalZone geoZone,
            @RequestParam(value = "geographicalZoneId", required = true) int geographicalZoneId,
            @RequestParam(value = "selectedMasterZoneIds", required = true, defaultValue = "0") String selectedMasterZoneIds) {

        int updateGeoZoneId = this.geoZoneService.updateGeoZone(geoZone, selectedMasterZoneIds);
        if (updateGeoZoneId != 0) {
            return "redirect:listGeographicalZone.htm?msg=Succesfully Updated Geographical Zone";
        } else {
            return "redirect:editGeographicalZone.htm?msg=Geographical Zone not successfully updated";
        }

    }
}
